package com.shine.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.bean.responses.ValidateResponse;
import com.shine.app.commons.StringUtils;
import com.shine.app.commons.UIHelper;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class ForgotPasswordActivity extends BaseActivity implements
		OnClickListener {

	@InjectView(R.id.head_back)
	private ImageView headBackImageView;
	@InjectView(R.id.email)
	private EditText emailEditText;
	@InjectView(R.id.submit)
	private Button submitbButton;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;

	Handler handler = new Handler() {
		public void handleMessage(Message message) {
			if (message.what == 1) {
				ValidateResponse response = (ValidateResponse) message.obj;
				if (response.isData()) {
					Intent intent = new Intent(ForgotPasswordActivity.this,
							ToastDialog_resetPasswordActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					ForgotPasswordActivity.this.startActivity(intent);
				} else {
					UIHelper.toastDialog(ForgotPasswordActivity.this,
							R.string.msg_password_reset_fail, R.string.msg_yes);
				}
			} else if (message.what == 0) {
				UIHelper.toastDialog(ForgotPasswordActivity.this,
						R.string.msg_password_reset_fail, R.string.msg_yes);
			} else if (message.what == -1) {
				Exception exception = (Exception) message.obj;
				UIHelper.toastDialogs(ForgotPasswordActivity.this,
						exception.getMessage(), R.string.msg_yes);
			}
			resetViewStatus(true);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgotpassword);
		Injector.get(this).inject();
		submitbButton.setOnClickListener(this);
		headBackImageView.setOnClickListener(this);
		progressBar.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_back:
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
			break;
		case R.id.submit:
			submitbButton.startAnimation(buttonAnimations);
			String email = emailEditText.getText().toString().trim();
			if (StringUtils.isEmpty(email)) {
				UIHelper.toastDialog(this, R.string.msg_check_enters,
						R.string.msg_yes);
				return;
			}
			if (!StringUtils.isEmail(email)) {
				UIHelper.toastDialog(this, R.string.msg_invalid_email_format,
						R.string.msg_yes);
				return;
			}
			resetPassword(email);
			break;

		default:
			break;
		}
	}

	private void resetPassword(final String email) {
		resetViewStatus(false);
		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					ValidateResponse response = appContext
							.forgotPassword(email);
					if (response == null || response.isHasError()) {
						message.what = 0;
					} else {
						message.what = 1;
						message.obj = response;
					}
				} catch (Exception e) {
					e.printStackTrace();
					message.what = -1;
					message.obj = e;
				}
				handler.sendMessage(message);
			}
		}.start();
	}

	private void resetViewStatus(boolean flag) {
		if (flag) {
			progressBar.setVisibility(View.GONE);
		} else {
			progressBar.setVisibility(View.VISIBLE);
		}
		headBackImageView.setClickable(flag);
		submitbButton.setClickable(flag);
		emailEditText.setEnabled(flag);
	}

}
